package com.gs.process.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gs.process.admin.service.ProcessService;
import com.gs.process.admin.utils.PageUtil;
import com.gs.process.model.Process;
import com.shallowUniverse.logs.annotation.AutoLog;
import com.shallowUniverse.model.Params;
import com.shallowUniverse.utils.QueryBuilder;
import com.shallowUniverse.web.BaseController;
import com.shallowUniverse.web.annotation.AutoReturn;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author: Shuai Guo
 * @description:
 * @date: 2021/9/27
 * @version: 1.0
 */
@Controller
@RequestMapping("/process")
public class ProcessController extends BaseController {

    @Resource
    private ProcessService processService;

    @AutoLog
    @AutoReturn
    @PostMapping("/find")
    public void find(@RequestBody Params<Process> params) {
        //拿到请求参数体
        Process body = params.getBody();
        //生成查询条件
        QueryWrapper<Process> wrapper = QueryBuilder.of(body).build();
        wrapper.lambda().ge(!ObjectUtils.isEmpty(body.getStartDate()), Process::getCreateDate, body.getStartDate());
        wrapper.lambda().ge(!ObjectUtils.isEmpty(body.getEndDate()), Process::getCreateDate, body.getEndDate());
        //定义结果
        List<Process> data;
        IPage<Process> page = null;
        //判断是否分页
        if (params.isPaging()) {
            data = processService.list(wrapper);
        } else {
            page = processService.page(PageUtil.create(params), wrapper);
            data = page.getRecords();
        }
        this.submit(true, data).setTotal(params.isPaging() ? (int) Objects.requireNonNull(page).getTotal() : data.size());
    }

    @AutoLog
    @AutoReturn
    @PostMapping("/save")
    public void save(@RequestBody Process process) {
        boolean result = processService.save(process);
        this.submit(result, process.getSid());
    }

    @AutoLog
    @AutoReturn
    @PostMapping("/update")
    public void update(@RequestBody Process process) {
        boolean result = processService.update(process);
        this.submit(result, process.getSid());
    }
}
